package com.adcc.loadsheet.billing.security;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.Collection;

/**
 * 用户信息实体
 * Created by zhaoml on 2020-04-01.
 */
public final class BillingUserDetails extends UserInfo implements Serializable, UserDetails {

    private Collection<? extends GrantedAuthority> authorities;

    public void setAuthorities(Collection<? extends GrantedAuthority> authorities) {
        this.authorities = authorities;
    }

    //自定义当前登录用户的详细信息(相当于将用户信息存入到session中的操作)
    public BillingUserDetails(UserInfo user) {
        this.setUsername(user.getUsername());
        this.setSuperAdmin(user.isSuperAdmin());
        this.setContractUserAbbrev(user.getContractUserAbbrev());
        this.setRoleAndPermission(user.getRoleAndPermission());
    }

    private static final long serialVersionUID = 1L;
    // 账号是否过期
    private boolean accountExpired = false;

    // 账号是否锁定
    private boolean accountLock = false;

    public void setAccountExpired(boolean accountExpired) {
        this.accountExpired = accountExpired;
    }

    public void setAccountLock(boolean accountLock) {
        this.accountLock = accountLock;
    }

    @Override
    // 帐户没有过期
    public boolean isAccountNonExpired() {
        return accountExpired;
    }

    @Override
    // 用户没有被锁定
    public boolean isAccountNonLocked() {
        return accountLock;
    }

    @Override
    // 密码没有过期
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    // 账号是否可用
    public boolean isEnabled() {
        return true;
    }

    @Override
    public String getPassword() {
        return null;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    @Override
    public String getUsername() {
        return super.getUsername();
    }

    @Override
    public boolean equals(Object o) {
        if (o == null) {
            return false;
        }
        if (o == this) {
            return true;
        }
        if (o instanceof BillingUserDetails) {
            BillingUserDetails e = (BillingUserDetails) o;
            if (e.getUsername().equals(this.getUsername())) {
                return true;
            }
        }
        return false;
    }

    @Override
    public int hashCode() {
        return this.getUsername().hashCode();
    }
}